package com.lowcode.platform.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lowcode.platform.entity.Role;
import com.lowcode.platform.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService extends ServiceImpl<RoleMapper, Role> {
    
    @Autowired
    private RoleMapper roleMapper;
    
    /**
     * 为角色分配权限
     * @param roleId 角色ID
     * @param permissionIds 权限ID列表
     */
    public void assignPermissions(Long roleId, List<Long> permissionIds) {
        // 1. 删除该角色之前的所有权限关联
        roleMapper.deleteRolePermissions(roleId);
        
        // 2. 如果有新的权限，批量插入
        if (permissionIds != null && !permissionIds.isEmpty()) {
            roleMapper.batchInsertRolePermissions(roleId, permissionIds);
        }
    }
    
    /**
     * 获取角色的权限ID列表
     * @param roleId 角色ID
     * @return 权限ID列表
     */
    public List<Long> getRolePermissionIds(Long roleId) {
        return roleMapper.getRolePermissionIds(roleId);
    }
}
